<?php

/**
 * 定义一棵树的类
 * @author whq
 *
 */
class Tree 
{
	/**
	 * 成长值
	 * @var int default 1
	 */
	private $_growthValue = 0;
	
	/**
	 * 树的成长次数
	 * @var int
	 */
	private $_growthTimes = 0;
	
	/**
	 * 果实生长值
	 * @var int
	 */
	private $_gainsGrowthValue = 0;
	
	/**
	 * 果实数量值
	 * @var int
	 */
	private $_gainsValue = 0;
	
	/**
	 * 标记是否死亡
	 * @var boolean default false
	 */
	private $_died = false;
	
	/**
	 * 标记是否干枯..
	 * @var boolean default false
	 */
	private $_wizened = false;
	
	/**
	 * 当前可砍伐次数
	 * @var int
	 */
	private $_canBeCut = 0;
	
	/**
	 * 标记被砍伐状态
	 * @var boolean
	 */
	private $_beCut = false;
	
	/**
	 * 标记结果
	 * @var string
	 */
	const  HAD_GAINS = '结果';
	
	//定义树的成长状态常量
	const SEED = '种子';
	const SEEDLING = '小苗';
	const LITTLE_TREE = '小树';
	const BIG_TREE = '大树';
	
	/**
	 * 状态等级常量
	 * @var array(
	 * 	{STATUS} => array(
	 * 		min : int,  //达到此状态的成长值下限
	 * 		max : int,	//达到此状态的成长值上限
	 * 	),...
	 * )
	 */
	private $_status = array(
		Tree::SEED => array(
			'min' => 0,
			'max' => 11,
		), 
		Tree::SEEDLING => array(
			'min' => 11,
			'max' => 51,
		), 
		Tree::LITTLE_TREE => array(
			'min' => 51,
			'max' => 201,
		), 
		Tree::BIG_TREE => array(
			'min' => 201,
			'max' => 300,
		), 
	);
	
	/**
	 * 树木状态可砍伐次数定义数组
	 * @var array( {状态} : int //次数 )
	 */
	private $_canBeCutNum = array(
		Tree::SEED => 0,
		Tree::SEEDLING => 0,
		Tree::LITTLE_TREE => 1,
		Tree::BIG_TREE => 10,
		Tree::HAD_GAINS => 10,
	);
	
	/**
     * @return the $_gainsValue
     */
    public function getGainsValue ()
    {
        return $this->_gainsValue;
    }

	/**
	 * @return the $_gainsGrowthValue
	 */
	public function getGainsGrowthValue() 
	{
		return $this->_gainsGrowthValue;
	}

	/**
	 * @return the $_died
	 */
	public function getDied() 
	{
		return $this->_died;
	}

	/**
	 * @param boolean $_died
	 */
	public function setDied($_died) 
	{
		if( $_died )
		{
			//当树木变成死亡状态时，果实会变为0
			$this->_gainsValue = 0;
		}
		$this->_died = $_died;
	}

	/**
	 * @return the $_wizened
	 */
	public function getWizened() 
	{
		return $this->_wizened;
	}

	/**
	 * @param boolean $_wizened
	 */
	public function setWizened($_wizened) 
	{
		if( $_wizened )
		{
			//如果树在变成干枯时，果实的数量会减少一半，并且向下取整
			$this->_gainsValue = floor( $this->_gainsValue / 2 );
		}
		$this->_wizened = $_wizened;
	}

	/**
	 * @return the $_canBeCut
	 */
	public function getCanBeCut() 
	{
		return $this->_canBeCut;
	}

	/**
	 * @param int $_canBeCut
	 */
	public function setCanBeCut($_canBeCut) 
	{
		$this->_canBeCut = $_canBeCut;
	}

	/**
	 * @param boolean $_beCut
	 */
	public function setBeCut($_beCut) 
	{
		$this->_beCut = $_beCut;
	}

	/**
     * @return the $_growthTimes
     */
    public function getGrowthTimes ()
    {
        return $this->_growthTimes;
    }

	/**
	 * 获取树的当前状态
	 * @return string
	 */
	public function getStatus()
	{
		//return $this->_growthValue <= 10 ? Tree::SEED : $this->_growthValue <= 50 ? Tree::SEEDLING : $this->_growthValue <= 200 ? Tree::LITTLE_TREE : $this->_gainsValue >0 ? Tree::BIG_TREE : Tree::HAD_GAINS;
		if( $this->_gainsValue > 0 )
		{
			return Tree::HAD_GAINS;
		}
		foreach ( $this->_status as $key => $value )
		{
			if( $this->_growthValue >= $value[ 'min' ] && $this->_growthValue <= $value[ 'max' ] )
			{
				return $key;
			}
		}
	}
	
	/**
	 * 树的成长机制
	 * @return boolean 是否更改状态
	 */
	public function grow()
	{
	    $this->_growthTimes++;
		$oldStatus = $this->getStatus();
		/*//大树前每次成长1，大树后每次0.5
		if( $this->_growthValue < $this->_status[Tree::BIG_TREE]['min'] )
		{
			$this->_growthValue += 1;
		}
		else 
		{
			$this->_growthValue += 1;
			//当树长到大树后，每次成长就会增加1点果实生长值
			$this->_gainsGrowthValue += 1;
			if( $this->_gainsGrowthValue >= 20 )
			{
				//当树的果实生长值到达20时，树的果实数量为成长值/4并向上取整
				$this->_gainsValue = ceil( $this->_gainsGrowthValue/4 );
				$this->_gainsGrowthValue = 0;
			}
		}*/
		/*树的成长值算法修改为log( 生长次数 , 1.0345 )，精度为小数点后1位，当树的成长值大于300时，停止生长*/
		$this->_growthValue = round( log( $this->getGrowthTimes() , 1.0349 ) , 1 );
		$this->_growthValue = $this->_growthValue > 300 ? 300 : $this->_growthValue;
	    if ( $this->getStatus() == Tree::BIG_TREE )
		{
		    //当树长到大树后，每次成长就会增加1点果实生长值
			$this->_gainsGrowthValue += 1;
			if( $this->_gainsGrowthValue >= 20 )
			{
				//当树的果实生长值到达20时，树的果实数量为成长值/4并向上取整
				$this->_gainsValue = ceil( $this->_gainsGrowthValue/4 );
				$this->_gainsGrowthValue = 0;
			}
		}
		$newStatus = $this->getStatus();
		$hadChangeStatus = ( $oldStatus != $newStatus );
		if( $hadChangeStatus )
		{
			$this->_canBeCut = $this->_canBeCutNum[ $newStatus ];
		}
		return $hadChangeStatus;
	}
	
	/**
	 * 收获果实
	 */
	public function getGains()
	{
		if( $this->_gainsValue >0 )
		{
			//果实数减1
			$this->_gainsValue -= 1;
			
			//果实生长系数减4，不减会有bug吧？
			$this->_gainsGrowthValue -= 4;
		}	
	}
}

?>